var _excluded = ["value", "onChange", "onOpenChange", "observer"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { DatePicker as AntDatePicker, TimePicker as AntTimePicker } from 'antd';
import locale from 'antd/es/date-picker/locale/zh_CN';
import toDayJs from 'dayjs';
import 'dayjs/locale/zh-cn';
import { compHoc, iup, IUPComponent } from "../util";
import { jsx as _jsx } from "react/jsx-runtime";
AntTimePicker.displayName = 'TimePicker';
var pickerFormat = {
  date: 'YYYY-MM-DD',
  year: 'YYYY',
  quarter: 'YYYY-[Q]Q',
  month: 'YYYY-MM',
  week: 'YYYY-wo'
};
function convertToDate(value, format) {
  if (format === pickerFormat.quarter && value.indexOf('-Q') > 0) {
    var parts = value.split('-Q');
    var year = parseInt(parts[0], 10);
    var quarter = parseInt(parts[1], 10);

    // 计算季度的月份
    var month = quarter * 3 - 2;

    // 创建 Day.js 对象
    return toDayJs(new Date(year, month, 1));
  }
  return toDayJs(value, format);
}
var getFormat = function getFormat(props, defaultFormat, type) {
  var _props$showTime;
  var showTime = (_props$showTime = props.showTime) !== null && _props$showTime !== void 0 ? _props$showTime : type === 'datetime';
  return props.format || pickerFormat[props.picker] || (showTime ? 'YYYY-MM-DD ' + (showTime.format || 'HH:mm:ss') : '') || defaultFormat;
};
function getDatePicker(Comp) {
  var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var dFormat = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'YYYY-MM-DD';
  return /*#__PURE__*/function (_IUPComponent) {
    _inherits(_class2, _IUPComponent);
    var _super = _createSuper(_class2);
    function _class2(props) {
      var _props$value;
      var _this;
      _classCallCheck(this, _class2);
      _this = _super.call(this, props);
      _defineProperty(_assertThisInitialized(_this), "onDatePickerChange", function (value) {
        if (_this.props.hasOwnProperty('value')) {
          // 外部受控
          _this.state.value = _this.getDayJs(value);
          _this.props.onChange && _this.props.onChange(_this.getValue());
        } else {
          _this.setValue(value);
        }
        _this.innerNotify([value], 'onChange').then();
      });
      _defineProperty(_assertThisInitialized(_this), "onOpenChange", function (open) {
        var _this$props$onOpenCha, _this$props, _window$event;
        if (!_this.props.hasOwnProperty('open')) {
          _this.setState({
            open: open
          });
        }
        (_this$props$onOpenCha = (_this$props = _this.props).onOpenChange) === null || _this$props$onOpenCha === void 0 || _this$props$onOpenCha.call(_this$props, open);

        // 明明可以单击选择，有人就是要双击，导致事件穿透，触发了弹窗下组件的点击事件，测试觉得这是个bug，临时这样处理
        var targetEl = (_window$event = window.event) === null || _window$event === void 0 ? void 0 : _window$event.target;
        if (!open && !_this.getShowTime() && targetEl) {
          var _targetEl$className, _targetEl$className$i;
          if ((targetEl === null || targetEl === void 0 || (_targetEl$className = targetEl.className) === null || _targetEl$className === void 0 || (_targetEl$className$i = _targetEl$className.indexOf) === null || _targetEl$className$i === void 0 ? void 0 : _targetEl$className$i.call(_targetEl$className, 'ant-picker-')) === 0) {
            // 如果是点击单元格
            document.body.style.pointerEvents = 'none';
            setTimeout(function () {
              document.body.style.pointerEvents = '';
            }, 250);
          }
        }
      });
      var defaultFormat = getFormat(props, dFormat, type);
      _this.state = {
        props: props,
        open: props.defaultOpen || props.open,
        defaultFormat: defaultFormat,
        value: _this.getDayJs((_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : props.defaultValue, defaultFormat)
      };
      return _this;
    }
    _createClass(_class2, [{
      key: "getShowTime",
      value: function getShowTime() {
        var _this$props$showTime;
        return (_this$props$showTime = this.props.showTime) !== null && _this$props$showTime !== void 0 ? _this$props$showTime : type === 'datetime';
      }
    }, {
      key: "setValue",
      value: function setValue(value, cb) {
        var _this2 = this;
        if (this.state.value === value) {
          return;
        }
        value = this.getDayJs(value);
        if (value && (iup.isArray(value) ? value.some(function (v) {
          return !v.isValid();
        }) : !value.isValid())) {
          return;
        }
        var newValue = value || null;
        this.setState({
          value: newValue
        }, function () {
          _this2.props.onChange && _this2.props.onChange(_this2.getValue());
          cb && cb(newValue);
        });
      }
    }, {
      key: "getValue",
      value: function getValue() {
        var _this3 = this;
        var value = this.getDayJs(this.state.value);
        if (!value) {
          return null;
        }
        return type === 'range' ? value.map(function (val) {
          return val ? val.format(_this3.state.defaultFormat) : null;
        }) : value.format(this.state.defaultFormat);
      }
    }, {
      key: "getRawValue",
      value: function getRawValue() {
        return this.getDayJs(this.state.value || null);
      }
    }, {
      key: "isOpened",
      value: function isOpened() {
        return !!this.state.open;
      }
    }, {
      key: "getDayJs",
      value: function getDayJs(value, defaultFormat) {
        var _this4 = this;
        if (iup.isArray(value)) {
          return value.map(function (v) {
            return _this4.getDayJs(v);
          });
        }
        if (iup.isDate(value)) {
          return toDayJs(value);
        }
        return !value || toDayJs.isDayjs(value) ? value : convertToDate(value, defaultFormat || this.state.defaultFormat);
      }
    }, {
      key: "render",
      value: function render() {
        var _ref = this.props,
          value = _ref.value,
          onChange = _ref.onChange,
          onOpenChange = _ref.onOpenChange,
          observer = _ref.observer,
          dataPickerProps = _objectWithoutProperties(_ref, _excluded);
        if (dataPickerProps.disabled) {
          dataPickerProps.allowClear = false;
          dataPickerProps.suffixIcon = null;
          dataPickerProps.placeholder = '';
          dataPickerProps.inputReadOnly = true;
        } else {
          var _dataPickerProps$allo;
          dataPickerProps.allowClear = (_dataPickerProps$allo = dataPickerProps.allowClear) !== null && _dataPickerProps$allo !== void 0 ? _dataPickerProps$allo : true;
          if (type === 'range' && iup.isString(dataPickerProps.placeholder)) {
            dataPickerProps.placeholder = [dataPickerProps.placeholder, dataPickerProps.placeholder];
          }
        }
        return /*#__PURE__*/_jsx(Comp, _objectSpread(_objectSpread({
          ref: this.outRef,
          style: {
            width: '100%'
          },
          locale: locale
        }, dataPickerProps), {}, {
          open: this.state.open,
          showTime: this.getShowTime(),
          value: this.getDayJs(this.state.value)
          // format={{
          //   format: this.state.defaultFormat,
          //   type: 'mask'
          // }}
          ,
          onOpenChange: this.onOpenChange,
          onChange: this.onDatePickerChange
        }));
      }
    }], [{
      key: "getDerivedStateFromProps",
      value: function getDerivedStateFromProps(nextProps, prevState) {
        var value = nextProps.value,
          format = nextProps.format,
          picker = nextProps.picker,
          showTime = nextProps.showTime,
          open = nextProps.open;
        // 当传入的value发生变化的时候，更新state, 这个props可以被当做缓存，仅用作判断
        var newState = {};
        if (open !== prevState.props.open) {
          newState.open = open;
          newState.props = nextProps;
        }
        if (value !== prevState.props.value) {
          newState.value = value;
          newState.props = nextProps;
        }
        if (format !== prevState.props.format || picker !== prevState.props.picker || showTime !== prevState.props.showTime) {
          newState.defaultFormat = getFormat(nextProps, dFormat, type);
          newState.props = nextProps;
        }
        return newState.props ? newState : null;
      }
    }]);
    return _class2;
  }(IUPComponent);
}
export var DatePicker = compHoc(getDatePicker(AntDatePicker));
export var DateTimePicker = compHoc(getDatePicker(AntDatePicker, 'datetime'));
export var RangePicker = compHoc(getDatePicker(AntDatePicker.RangePicker, 'range'));
export var TimePicker = compHoc(getDatePicker(AntTimePicker, '', 'HH:mm:ss'));